define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index' + location.search,
                    add_url: 'coupon/add',
                    edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'coupon_name', title: __('Coupon_name'), operate: 'like'},
                        {field: 'coupon_amount', title: __('Coupon_amount'), operate:'BETWEEN'},
                        // {field: 'days', title: __('Days')},
                        {field: 'limit_type', title: __('Limit_type'), searchList: Config.limitTypeList, formatter: Table.api.formatter.label},
                        {field: 'coupon_limit', title: __('Coupon_limit'), operate: false},
                        {field: 'type', title: __('Type'), searchList: Config.typeList, formatter: Table.api.formatter.label},
                        {field: 'start_time', title: __('Start_time'), formatter: Table.api.formatter.datetime, datetimeFormat:'YYYY-MM-DD'},
                        {field: 'coupon_time', title: __('Coupon_time'), formatter: Table.api.formatter.datetime, datetimeFormat:'YYYY-MM-DD'},
                        {field: 'coupon_rate', title: __('Coupon_rate'), operate: false},
                        {field: 'is_eject', title: __('Is_eject'), searchList: {1:'否',2:'是'}, formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {1:'上架',2:'下架'}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'is_bao', title: __('Is_bao'), searchList: {1:'已报名',2:'未报名'}, formatter: Table.api.formatter.label, visible:Config.visible},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name:'join',
                                    text:'报名',
                                    title:'报名',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url: 'coupon/join',
                                    confirm: '确认报名？',
                                    visible: function (row) {
                                        if(row.is_bao == 2 && Config.group_id == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("报名成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.close(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'drop',
                                    text:'取消报名',
                                    title:'取消报名',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url: 'coupon/drop',
                                    confirm: '确认取消报名？',
                                    visible: function (row) {
                                        if(row.is_bao == 1 && Config.group_id == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("取消报名成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.close(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        join: function () {
            Controller.api.bindevent();
        },
        drop: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});